<?php

class Painel_ParametrosController extends App_Controller_Action
{
    private $_table     = 'PARAMETROS';
    private $_pk        = 'PAR_ID';
    private $_order_col = 'PAR_ID';
    private $_order     = 'ASC';
    private $_sta_col   = 'PAR_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'PAR_FILE'; 
    public  $dir = 'public/upload//';
    public  $db = '';

    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Painel_Form_Parametros();
    }

    public function indexAction() {
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
    
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_pdr.js');
        
        //$this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ckeditor/ckeditor.js');
        //$option = array('width'=>'70%');
        //echo $this->view->CkeditorHelper('PAR_DADCHQ',$option);
        //echo $this->view->CkeditorHelper('PAR_DADDEPCNT',$option);
        
        $this->view->form = $this->form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk);

                    $data = $this->form->getValues();

                    unset($data['img']);
                    unset($data[$this->_file_col]);
                    unset($data['br']);

                    $this->db->beginTransaction();
                    if($id == 0){
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();   
                        //imagem::rename($id,$this->dir,$returnImg['filenew']);
                    }
                    else $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);
                    
                    $this->db->commit();

                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->form->populate($formData);
                $this->db->rollBack();
            }
            
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->db->fetchRow('SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $msg = '';
        $id = $this->_getParam('id', 0);
        if((int) $id > 0 ) {   
            $this->db->beginTransaction();
            try {
                $ok = (bool) $this->db->delete($this->_table, $this->_pk . ' = ' . $id);
                $this->db->commit();
            } catch(Exception $ex) {
                //echo getMessage();
                $ok = FALSE;
                $msg = MSG_DELETE;
                $this->db->rollBack();
            }          
        }     
        echo json_encode(array('ok' => $ok, 'msg'=> $msg));
    }

}

